package com.changgou.business.controller;

import com.changgou.business.pojo.Activity;
import com.changgou.business.service.ActivityService;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activity")
@CrossOrigin
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 添加活动
     * @param activity
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Activity activity){
        try {
            //设置活动状态,默认进行中
            activity.setStatus("活动进行中");
            activityService.add(activity);
            return new Result(true,StatusCode.OK,"添加活动成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"添加活动失败");
        }
    }

    /**
     * 分页条件查询活动列表
     * @param page
     * @param size
     * @param condition
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result findByPage(@PathVariable("page") int page,@PathVariable("size") int size,@RequestParam("condition") String condition){
        try {
            PageResult<Activity> pageInfo = activityService.findByPage(page,size,condition);
            return new Result(true,StatusCode.OK,"查询活动列表成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询活动列表失败");
        }
    }

    /**
     * 上下架活动
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public Result updateMarketable(@PathVariable("id") Integer id){
        try {
            activityService.updateMarketable(id);
            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }
    /**
     * 删除活动
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        activityService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
